#coding:utf-8

import gzip
import pickle

import numpy as np

from  nn.tools.mlogging import logging


@logging(info='function load_data......')
def load_data(path="../data/mnist.pkl.gz"):
    with gzip.open(path,'rb') as f:
        training_data,validation,test_data=pickle.load(f,encoding='iso-8859-1')
    return (training_data,validation,test_data)

@logging(info='function load_data_wrapper......')
def load_data_wrapper():
    tr_d,va_d,te_d=load_data()
    training_inputs=[np.reshape(x,(784,1)) for x in tr_d[0]]
    training_results=[vectorized_result(y) for y in tr_d[1]]
    training_data=list(zip(training_inputs,training_results))
    validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
    validation_data = zip(validation_inputs, va_d[1])
    test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
    test_data = list(zip(test_inputs, te_d[1]))
    return (training_data,validation_data,test_data)

def vectorized_result(res,shape=(10,1)):
    '''
        将结果转化为向量
    :param res:
    :return:
    '''
    e=np.zeros(shape)
    e[res]=1.0
    return e

if __name__=="__main__":
    t_in,t_label,test_data=load_data_wrapper()
    print(t_in[0])
    print(t_label[0])
    print('---------------------')
    print( len(test_data))
    for x,y in test_data:
        print(x)
        print(y)
        break